define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'work/protect/index' + location.search,
                    add_url: 'work/protect/add',
                    //edit_url: 'work/protect/edit',
                    del_url: 'work/protect/del',
                    multi_url: 'work/protect/multi',
                    import_url: 'work/protect/import',
                    table: 'work_warranty',
                }
            });

            var table = $("#table");

            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //这里我们手动设置底部的值
                $("#final_price_sum").text(data.final_price_sum);
                $("#profit_sum").text(data.profit_sum);
                $("#receivable_sum").text(data.receivable_sum);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'workID', title: __('WorkID'), operate: 'LIKE'},
                        {field: 'hospital.name', title: __('Hospital.name'), operate: 'LIKE', formatter: Table.api.formatter.normal},
                        {field: 'equipment.name', title: __('Equipment.name'), operate: 'LIKE'},
                        {field: 'equipment.marking', title: __('Equipment.marking'), operate: 'LIKE'},
                        {field: 'equipment.serial', title: __('Equipment.serial'), operate: 'LIKE'},
                        {field: 'sale.nickname', title: __('Sale.nickname'), operate: 'LIKE'},
                        {field: 'engineer.nickname', title: __('Engineer.nickname'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"7":__('Status 7')}, formatter: Table.api.formatter.status},
                        {field: 'billing_data', title: __('Billing_data'), searchList: {"0":__('Billing_data 0'),"1":__('Billing_data 1')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'final_price', title: __('Final_price'), operate:false},
                        {field: 'profit', title: __('Profit'), operate: false},
                        {field: 'collect_data', title: __('collect_data'), searchList: {"0":__('Collect_data 0'),"1":__('Collect_data 1')}, formatter: Table.api.formatter.normal},


                        /*{field: 'product', title: __('Product'), operate: 'LIKE'},
                        {field: 'device', title: __('Device'), operate: 'LIKE'},*/
                        {field: 'contacts', title: __('Contacts'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},

                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE', formatter: Table.api.formatter.normal},
                        //{field: 'open_data', title: __('Open_data'), searchList: {"0":__('Open_data 0'),"1":__('Open_data 1')}, formatter: Table.api.formatter.toggle},
                        {field: 'buttons', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: 'xshk',
                                    title: __('回款记录'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '回款记录',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='work/xshk?warranty_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'cost',
                                    title: __('成本'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '成本',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='cost/cost?warranty_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'offer',
                                    title: __('报价'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '报价',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='work/offer?warranty_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'project',
                                    title: __('方案'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '方案',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='work/project?warranty_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'repair',
                                    title: __('维修'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '维修',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='work/repair?warranty_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'maintain',
                                    title: __('保养'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '保养',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='work/maintain?warranty_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'maintain',
                                    title: __('工作流'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '工作流',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='workflow?warranty_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'service',
                                    title: __('维修记录'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '维修记录',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='work/service?warranty_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'report',
                                    title: __('服务报告'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    text: '服务报告',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='report/index?warranty_id='+row.id;
                                        return url;
                                    },
                                }
                            ],
                            formatter: Table.api.formatter.buttons, operate: false
                        },
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {
                                name: 'loss',
                                title:'亏损',
                                text: '亏损',
                                classname: 'btn btn-xs btn-primary btn-magic btn-ajax',
                                icon: '',
                                confirm: '确定改为亏损？',
                                url: 'work/warranty/loss',
                                success: function (data, ret) {
                                    table.bootstrapTable('refresh', {});
                                    return false;
                                },
                                error: function (data, ret) {
                                    table.bootstrapTable('refresh', {});
                                    return false;
                                },
                                visible: function (row) {
                                    // console.log(row)
                                    var check=$(".nav-tabs .active a").attr("data-value");
                                    if(check==3 || row.is_loss==1 || check==7 ){
                                        return false;
                                    }
                                    return true;

                                },
                            },
                                {
                                    name: 'add_image',
                                    title: __('报价'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    text: __('报价'),   icon: '',
                                    url: 'work/offer/add'
                                },
                                {
                                    name: 'see',
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    text: __('详情'),   icon: '',
                                    url: 'work/warranty/see'
                                },
                                {
                                    name: 'appoint',
                                    title: __('指派'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    text: __('指派'),   icon: '',
                                    url: 'work/warranty/appoint'
                                },
                                {
                                    name: 'final',
                                    extend:'data-area=["800px","400px"]',
                                    title: __('成交价'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    text: __('成交价'),   icon: '',
                                    url: 'work/warranty/final'
                                },
                                {
                                    name: 'billing',
                                    title: __('开票'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    text: __('开票'),   icon: '',
                                    url: 'work/warranty/billing'
                                }
                            ],
                            events: Table.api.events.operate,
                            formatter:function (value, row, index){
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if(row["engineer_id"]!=0)
                                {
                                    $(table).data("operate-appoint", null);
                                    that.table = table;
                                }
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            //Controller.api.bindevent();
            $(document).on("fa.event.appendfieldlist", '.btn-append', function(e,obj){
                //通用的表单组件事件绑定和组件渲染
                Form.events.datetimepicker(obj);
            });
            $("#c-department_id").data("params", function(){
                const hospital_id = $("#c-hospital_id").val();
                return {hospital_id: hospital_id};
            });
            $("#c-equipment_id").data("params", function(){
                const department_id = $("#c-department_id").val();
                return {department_id: department_id};
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        appoint: function () {
            Controller.api.bindevent();
        },
        billing: function () {
            Controller.api.bindevent();
        },
        final: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});